package com.mesnac.comm.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


/**
 * 
* @ClassName: BaseDao
* @Description: 基本的baseDao接口
* @author chilei chil07@mesnac.com
* @date Sep 3, 2012 4:51:33 PM
*
* @param <T>
 */
public abstract class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T>{
	
	public List<T> getByPage(final String hql, final int beginIndex, final int everyPage) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException,SQLException{
				Query query = session.createQuery(hql);
				query.setFirstResult(beginIndex);
				query.setMaxResults(everyPage);
				return query.list();
			}
		});
		
	}

	private Class clazz;
	
	public BaseDao(){
		this.clazz = this.getTClass();
	}
	
	private Class getTClass(){
		return (Class)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}
	/*

	@Override
	protected HibernateTemplate createHibernateTemplate(
			SessionFactory sessionFactory) {
		// TODO Auto-generated method stub
		return super.createHibernateTemplate(sessionFactory);
	}

	@Override
	protected void initDao() throws Exception {
		// TODO Auto-generated method stub
		super.initDao();
	}
	*/
	public void delete(T entity) {
		// TODO Auto-generated method stub
		
		super.getHibernateTemplate().delete(entity);
	}

	public List<T> getAll() {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(this.clazz));
	}
	

	public List<T> getAllWithCache() {
		// TODO Auto-generated method stub
		super.getHibernateTemplate().setCacheQueries(true);
		return super.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(this.clazz));
	}

	public List<T> getByExample(T example) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().findByExample(example);
	}

	public T getById(Serializable id) {
		// TODO Auto-generated method stub
		return (T)super.getHibernateTemplate().get(this.clazz, id);
	}

	public List<T> getByProperty(String property, Object value) {
		// TODO Auto-generated method stub
		DetachedCriteria c = DetachedCriteria.forClass(clazz);
		c.add(Restrictions.eq(property, value));
		return super.getHibernateTemplate().findByCriteria(c);
	}

	public Serializable save(T entity) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().save(entity);
		
	}

	public void update(T entity) {
		// TODO Auto-generated method stub
		super.getHibernateTemplate().update(entity);
		
	}

	public T load(Class entityClass, Serializable id) {
		// TODO Auto-generated method stub
		return (T)super.getHibernateTemplate().load(entityClass, id);
	}
	

	
}
